package com.lei.yohojava_002;

import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.EncryptUtils;
import com.lei.library_mvp_retrofit.http.bean.LoginBean;
import com.lei.library_mvp_retrofit.http.rsa.RSAUtils;
import com.lei.library_mvp_retrofit.mvp.view.BaseActivity;
import com.lei.yohojava_002.contract.YoHoContract;
import com.lei.yohojava_002.model.YoHoModel;
import com.lei.yohojava_002.presenter.YoHoPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class LoginActivity extends BaseActivity<YoHoPresenter> implements YoHoContract.LoginView {
    private ImageView loginBack;
    private Button loginReg;
    private Spinner loginSpinner;
    private EditText loginPhone;
    private LinearLayout loginLlPhone;
    private EditText loginYzmEt;
    private Button loginYzmBt;
    private LinearLayout loginLlPwd;
    private EditText loginPwdEt;
    private CheckBox loginPwdEye;
    private CheckBox loginUserPwdBt;
    private TextView loginWjPwd;
    private Button btLogin;
    private ImageView loginWechat;
    private ImageView loginQq;
    private ImageView loginZhifubao;
    private ImageView loginWeibo;
    private ImageView loginPhoneIv;
    private String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJa4C5IKvNRcLWXiLFcF4F+i1S2QAusCMszlQeJV84UetEkczjUdJ4dWbnpRkeAmXCTzRHyO67XKS6GSCuKVO/sf7cyll0i6e+d0MSWB2CTxojYingZSV6ZQO8K1Z3fJyFYSHiRhDwJ4idC80zTyKagsWV29uNa38iQYr4FwbNqZAgMBAAECgYAxV1k6W1eMMg0OsKeRabQVuwoNG3tJEnQtDdSu0zKg3vdohAyh6MR7EvmiA7g86HH8CsPd/y/9WJe/8j6sBO0Ye9gt7eyQ2NiwWvlTuwNmngcSTapVvVI6NEyJFMfQt9PB1EHLNAXlz8jtJUyA7C48jReQD9p/SzAP0VxG7lwyMQJBAOjE7hAZ/6fyP3DB1fG7jr9gONZcz3TUaqx6BUn4GKZnckW08ht9Xqcqft5Hthu8BbLM9ptQ0U8QZekrJwD6ya0CQQClwstZMPu8jLhsgugVwodcG1mPEOiw9Yjnmt9+WTI07Ll2uFv//hRXBnahBBnZbucUYEbUY3kqUX9b3e9TmEodAkEAybPMbxt4VDoxCy6Mi/pxChkBZ4/pHV3sSiU6bAyWn6vIc+sGWRfca5MBePA/N+1IKtY9Y/02QwL8rH5+P/URyQJAL/hdjORGFdzLimuf6pwvPBKWKncEQCHuisghIZmClBpl2duklELddAnkztg2+tvDd/wcw14+NGb9aoKhvhl2aQJAbvcgoPU+xs0CjeexH+TS2S/jKkTRpvP2CpPK/k71m13xWdE8RtMkYY1measRmlIwOfWze7ll/PGT4dxWf31FNg==\n";


    private int yzmTime = 61;
    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        loginBack = (ImageView) findViewById(R.id.login_back);
        loginReg = (Button) findViewById(R.id.login_reg);
        loginSpinner = (Spinner) findViewById(R.id.login_spinner);
        loginPhone = (EditText) findViewById(R.id.login_phone);
        loginLlPhone = (LinearLayout) findViewById(R.id.login_ll_phone);
        loginYzmEt = (EditText) findViewById(R.id.login_yzm_et);
        loginYzmBt = (Button) findViewById(R.id.login_yzm_bt);
        loginLlPwd = (LinearLayout) findViewById(R.id.login_ll_pwd);
        loginPwdEt = (EditText) findViewById(R.id.login_pwd_et);
        loginPwdEye = (CheckBox) findViewById(R.id.login_pwd_eye);
        loginUserPwdBt = (CheckBox) findViewById(R.id.login_user_pwd_bt);
        loginWjPwd = (TextView) findViewById(R.id.login_wj_pwd);
        btLogin = (Button) findViewById(R.id.bt_login);
        loginWechat = (ImageView) findViewById(R.id.login_wechat);
        loginQq = (ImageView) findViewById(R.id.login_qq);
        loginZhifubao = (ImageView) findViewById(R.id.login_zhifubao);
        loginWeibo = (ImageView) findViewById(R.id.login_weibo);
        loginPhoneIv = (ImageView) findViewById(R.id.login_phone_iv);
    }

    @Override
    public void initData() {
        mPresenter = new YoHoPresenter(new YoHoModel(),this);
        loginPhone.addTextChangedListener(new MyEditText());
        loginPwdEt.addTextChangedListener(new MyEditText());
        loginYzmEt.addTextChangedListener(new MyEditText());
        //点击错号退出登录页面
        loginBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //没有账号进入注册页面
        loginReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                finish();
            }
        });
        //点击获取验证码
        loginYzmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (loginPhone.getText().length() != 0){
                    yzmTime = 61;
//                loginYzmBt.setEnabled(false);
                    loginYzmBt.setBackgroundResource(R.drawable.bt_yzm2);
                    loginYzmBt.setTextColor(Color.BLACK);
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new TimerTask() {
                                @Override
                                public void run() {
                                    if (yzmTime>0){
                                        --yzmTime;
                                        loginYzmBt.setText("重新获取 "+yzmTime);
                                        loginYzmBt.setEnabled(false);
                                    }else {
                                        loginYzmBt.setText("获取验证码");
                                        loginYzmBt.setTextColor(Color.WHITE);
                                        loginYzmBt.setBackgroundResource(R.drawable.bt_yzm);
                                        loginYzmBt.setEnabled(true);
                                    }
                                }
                            });
                        }
                    },0,1000);

                    int i1 = new Random().nextInt(9);
                    char i2 = (char) (new Random().nextInt(26)+'A');
                    int i3 = new Random().nextInt(9);
                    char i4 = (char) (new Random().nextInt(26)+'a');
                    int i5 = new Random().nextInt(9);
                    char i6 = (char) (new Random().nextInt(26)+'A');

                    loginYzmEt.setText(i1+""+i2+""+i3+""+i4+""+i5+""+i6);
                }else {
                    Toast.makeText(LoginActivity.this, "请先输入手机号", Toast.LENGTH_SHORT).show();
                }
            }
        });



        loginUserPwdBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (loginUserPwdBt.isChecked()){
                    loginLlPhone.setVisibility(View.GONE);
                    loginLlPwd.setVisibility(View.VISIBLE);
                    loginWjPwd.setVisibility(View.VISIBLE);
                    loginPhoneIv.setImageResource(R.drawable.y_icon_new_1);
                    loginUserPwdBt.setText("验证码登录");
                }else {
                    loginLlPhone.setVisibility(View.VISIBLE);
                    loginLlPwd.setVisibility(View.GONE);
                    loginWjPwd.setVisibility(View.GONE);
                    loginUserPwdBt.setText("账户密码登录");
                    loginPhoneIv.setImageResource(R.drawable.icon_new_phone);
                }
            }
        });

        loginPwdEye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (loginPwdEye.isChecked()){
                    loginPwdEye.setButtonDrawable(R.drawable.login_password_see_icon);
                    loginPwdEt.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }else {
                    loginPwdEye.setButtonDrawable(R.drawable.login_password_unsee_icon);
                    loginPwdEt.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                }
            }
        });

        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = loginPhone.getText().toString().trim();
                String pwd = loginPwdEt.getText().toString().trim();
                //TODO RSA加密
                byte[] buff = EncryptUtils.encryptRSA(pwd.getBytes(),
                        Base64.decode(privateKey.getBytes(), Base64.DEFAULT),
                        false, "RSA/ECB/PKCS1Padding");
                //base64
                String pwdStr = Base64.encodeToString(buff, Base64.DEFAULT);
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("username",name);
                    jsonObject.put("password",pwdStr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.e("ljl", "pwdStr:" + jsonObject.toString());
                mPresenter.login(jsonObject.toString());
            }
        });
    }

    @Override
    public void inJect() {

    }

    @Override
    public void getLogin(LoginBean loginBean) {
        String statues = loginBean.getStatues();
        if (statues.equals("1")){
            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(LoginActivity.this,MainActivity.class));
            finish();
        }
    }

    class MyEditText implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //输入前
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //输入中

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //输入后
            if (loginYzmEt.getText().length()!=0&&loginPhone.getText().length()!=0||loginPhone.getText().length()!=0&&loginPwdEt.getText().length()!=0){
                btLogin.setEnabled(true);
                btLogin.setBackgroundColor(Color.BLACK);
            }else {
                btLogin.setEnabled(false);
                btLogin.setBackgroundColor(Color.GRAY);
            }
        }
    }
}
